<?php

/**
 * This is the model class for table "{{help_cat}}".
 *
 * The followings are the available columns in table '{{help_cat}}':
 * @property integer $zhc_id
 * @property string $zhc_title
 * @property string $zhc_desc
 * @property integer $zhc_parentid
 * @property string $zhc_childid
 * @property integer $zhc_topid
 * @property integer $zhc_listorder
 * @property integer $inputitme
 * @property integer $updatetime
 */
class HelpCat extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return HelpCat the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{help_cat}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zhc_parentid, zhc_topid, zhc_listorder, inputitme, updatetime', 'numerical', 'integerOnly'=>true),
			array('zhc_title', 'length', 'max'=>100),
			array('zhc_desc, zhc_childid', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zhc_id, zhc_title, zhc_desc, zhc_parentid, zhc_childid, zhc_topid, zhc_listorder, inputitme, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zhc_id' => 'Zhc',
			'zhc_title' => 'Zhc Title',
			'zhc_desc' => 'Zhc Desc',
			'zhc_parentid' => 'Zhc Parentid',
			'zhc_childid' => 'Zhc Childid',
			'zhc_topid' => 'Zhc Topid',
			'zhc_listorder' => 'Zhc Listorder',
			'inputitme' => 'Inputitme',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zhc_id',$this->zhc_id);
		$criteria->compare('zhc_title',$this->zhc_title,true);
		$criteria->compare('zhc_desc',$this->zhc_desc,true);
		$criteria->compare('zhc_parentid',$this->zhc_parentid);
		$criteria->compare('zhc_childid',$this->zhc_childid,true);
		$criteria->compare('zhc_topid',$this->zhc_topid);
		$criteria->compare('zhc_listorder',$this->zhc_listorder);
		$criteria->compare('inputitme',$this->inputitme);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " zhc_listorder DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>100,
			)
		));
	}
	
	/**
	 * 保存前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			if($this->zhc_parentid!=0){
				//取分类顶级ID
				$parent = $this->findByPk($this->zhc_parentid);
				$this->zhc_topid = $parent->zhc_parentid;
			}
			$this->inputitme = time();
		}else{
			$this->updatetime = time();
		}
		return true;
	}
	
	/**
	 * 保存后的操作
	 * 1.更新对应父分类的子分类
	 */
	protected function afterSave(){
		if($this->isNewRecord){
			if($this->zhc_parentid>0){
				//分类父级
				$parent = $this->findByPk($this->zhc_parentid);
				if($parent->zhc_parentid>0){
					//分类顶级
					$top = $this->findByPk($parent->zhc_parentid);
					if($top->zhc_childid==null){
						$top->zhc_childid = $this->zhc_id;
					}else{
						$top->zhc_childid = $top->zhc_childid.','.$this->zhc_id;
					}
					$top->save();
				}
				if($parent->zhc_childid==null){
					$parent->zhc_childid = $this->zhc_id;
				}else{
					$parent->zhc_childid = $parent->zhc_childid.','.$this->zhc_id;
				}
				$parent->save();
			}
		}
		//清空缓存
		$this->DelCatcache();
		return true;
	}
	
	/**
	 * 删除前的操作
	 * 更新相关父分类的子分类
	 */
	protected function beforeDelete(){
		if($this->zhc_parentid>0){
			//分类父级
			$parent = $this->findByPk($this->zhc_parentid);
			if($parent->zhc_parentid>0){
				//分类顶级
				$top = $this->findByPk($parent->zhc_parentid);
				$top->zhc_childid = str_replace(','.$this->zhc_id,'',$top->zhc_childid);
				$top->zhc_childid = str_replace($this->zhc_id.',','',$top->zhc_childid);
				$top->zhc_childid = str_replace($this->zhc_id,'',$top->zhc_childid);
				$top->save();
			}
			$parent->zhc_childid = str_replace(','.$this->zhc_id,'',$parent->zhc_childid);
			$parent->zhc_childid = str_replace($this->zhc_id.',','',$parent->zhc_childid);
			$parent->zhc_childid = str_replace($this->zhc_id,'',$parent->zhc_childid);
			$parent->save();
		}
		return true;
	}
	
	/**
	 * 资讯分类缓存
	 */
	public function getHelpCategoryJson(){
		//如果存在缓存，调用缓存数据
		if($json = Yii::app()->filecache->get('helpcat')){
			return $json;
		}else{
			$criteria = new CDbCriteria;
			$criteria->compare('zhc_parentid','0');
			$data = HelpCat::model()->findAll($criteria);
			$item['0'] = $data = CHtml::listData($data,'zhc_id','zhc_title');
			foreach($data as $key=>$val){
				$criteria = new CDbCriteria;
				$criteria->compare('zhc_parentid',$key);
				$data2 = HelpCat::model()->findAll($criteria);
				if(!empty($data2))
					$item['0,'.$key] = $data2 = CHtml::listData($data2,'zhc_id','zhc_title');
				foreach($data2 as $key2=>$val2){
					$criteria = new CDbCriteria;
					$criteria->compare('zhc_parentid',$key2);
					$data3 = HelpCat::model()->findAll($criteria);
					if(!empty($data3))
						$item['0,'.$key.",".$key2] = $data3 = CHtml::listData($data3,'zhc_id','zhc_title');
				}
			}
			$json = json_encode($item);
			//设置缓存
			Yii::app()->filecache->set('helpcat',$json,3600*24);
			return $json;
		}
	}
	
	//清空缓存
	public function DelCatcache(){
		Yii::app()->filecache->delete('helpcat');
	}
	
	protected function afterDelete(){
		//清空缓存
		$this->DelCatcache();
		return true;
	}
}